from datetime import datetime
from typing import List

from sqlalchemy.orm import Session

from app.models import BroadcastPresets
from app.schemas.broadcast_presets import BroadcastPresetsCreate, BroadcastPresetsUpdate
from app.crud.base import CRUDBase

class CRUDDataSet(CRUDBase[BroadcastPresets, BroadcastPresetsCreate, BroadcastPresetsUpdate]):
    def get_by_category_name(self, db: Session, category_name: str) -> List[BroadcastPresets]:
        """
        根据分类名称查询个性化词汇。
        """
        return db.query(BroadcastPresets).filter(BroadcastPresets.category_name == category_name).all()

    def get_by_date_range(self, db: Session, start_date: datetime, end_date: datetime) -> List[BroadcastPresets]:
        """
        根据日期范围查询个性化词汇。
        """
        return db.query(BroadcastPresets).filter(BroadcastPresets.created_at >= start_date,
                                                       BroadcastPresets.created_at <= end_date).all()

    def get_unique_categories(self, db: Session) -> List[str]:
        """
        获取不重复的分类名称列表。
        """
        result = db.query(BroadcastPresets.category_name).distinct().all()
        return [category[0] for category in result]

# 实例化 CRUD 对象，以便在路由（route）中使用
broadcast_presets = CRUDDataSet(BroadcastPresets)
